package org.szgb.console.sys.custom.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysCustomFormItemModel extends BeanEntity<SysCustomFormItemModel> {
    @Serial
    private static final long serialVersionUID = 1L;
    private SysCustomFormModel model;
    private Integer type;
    private String label;
    private String options;
    private Boolean required;
    private Integer sort;

    public SysCustomFormItemModel() {
    }

    public SysCustomFormItemModel(String id) {
        this.setId(id);
    }

    public SysCustomFormItemModel(SysCustomFormItemModel.SysCustomFormItemModelFindDto SysCustomFormItemModelFindDto){
        this.setId(SysCustomFormItemModelFindDto.getId());
        if(SysCustomFormItemModelFindDto.getModelId() != null){
            this.model = new SysCustomFormModel(SysCustomFormItemModelFindDto.getModelId());
        }
        this.type = SysCustomFormItemModelFindDto.getType();
        this.label = SysCustomFormItemModelFindDto.getLabel();
        this.options = SysCustomFormItemModelFindDto.getOptions();
        this.required = SysCustomFormItemModelFindDto.getRequired();
        this.sort = SysCustomFormItemModelFindDto.getSort();
        this.setPageDto(SysCustomFormItemModelFindDto.getPageDto());
    }

    public SysCustomFormItemModel(SysCustomFormItemModel.SysCustomFormItemModelSaveDto SysCustomFormItemModelSaveDto){
        this.setId(SysCustomFormItemModelSaveDto.id);
        if(SysCustomFormItemModelSaveDto.modelId != null){
            this.model = new SysCustomFormModel(SysCustomFormItemModelSaveDto.modelId);
        }
        this.type = SysCustomFormItemModelSaveDto.type;
        this.label = SysCustomFormItemModelSaveDto.label;
        this.options = SysCustomFormItemModelSaveDto.options;
        this.required = SysCustomFormItemModelSaveDto.required;
        this.sort = SysCustomFormItemModelSaveDto.sort;
    }

    public SysCustomFormItemModel.SysCustomFormItemModelView toSysCustomFormItemModelView() {
        return new SysCustomFormItemModel.SysCustomFormItemModelView(this);
    }

    @Data
    @Schema(title = "自定义表单子项模板信息", description = "自定义表单子项模板信息")
    public static class SysCustomFormItemModelView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "模板id")
        private String modelId;
        @Schema(description = "类型（字典）")
        private Integer type;
        @Schema(description = "标题")
        private String label;
        @Schema(description = "选项（多个逗号隔开）")
        private String options;
        @Schema(description = "是否必填")
        private Boolean required;
        @Schema(description = "排序")
        private Integer sort;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysCustomFormItemModelView(SysCustomFormItemModel SysCustomFormItemModel) {
            this.id = SysCustomFormItemModel.getId();
            if(SysCustomFormItemModel.getModel() != null){
                this.modelId = SysCustomFormItemModel.getModel().getId();
            }
            this.type = SysCustomFormItemModel.getType();
            this.label = SysCustomFormItemModel.getLabel();
            this.options = SysCustomFormItemModel.getOptions();
            this.required = SysCustomFormItemModel.getRequired();
            this.sort = SysCustomFormItemModel.getSort();
            this.createdAt = SysCustomFormItemModel.getCreatedAt();
            this.updatedAt = SysCustomFormItemModel.getUpdatedAt();
            if (SysCustomFormItemModel.getCreatedBy() != null) {
                this.createdById = SysCustomFormItemModel.getCreatedBy().getId();
                this.createdByName = SysCustomFormItemModel.getCreatedBy().getName();
            }
            if (SysCustomFormItemModel.getUpdatedBy() != null) {
                this.updatedById = SysCustomFormItemModel.getUpdatedBy().getId();
                this.updatedByName = SysCustomFormItemModel.getUpdatedBy().getName();
            }
        }

        public static List<SysCustomFormItemModel.SysCustomFormItemModelView> getViewListFromList(List<SysCustomFormItemModel> SysCustomFormItemModelList) {
            return SysCustomFormItemModelList.stream().map(SysCustomFormItemModel.SysCustomFormItemModelView::new).toList();
        }
    }

    @Data
    @Schema(title = "自定义表单子项模板查询表单", description = "自定义表单子项模板查询参数")
    public static class SysCustomFormItemModelFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "模板id")
        private String modelId;
        @Schema(description = "类型（字典）")
        private Integer type;
        @Schema(description = "标题")
        private String label;
        @Schema(description = "选项（多个逗号隔开）")
        private String options;
        @Schema(description = "是否必填")
        private Boolean required;
        @Schema(description = "排序")
        private Integer sort;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysCustomFormItemModel toSysCustomFormItemModel() {
            return new SysCustomFormItemModel(this);
        }
    }

    @Data
    @Schema(title = "自定义表单子项模板保存表单", description = "自定义表单子项模板保存参数")
    public static class SysCustomFormItemModelSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "模板id")
        private String modelId;
        @Schema(description = "类型（字典）")
        private Integer type;
        @Schema(description = "标题")
        private String label;
        @Schema(description = "选项（多个逗号隔开）")
        private String options;
        @Schema(description = "是否必填")
        private Boolean required;
        @Schema(description = "排序")
        private Integer sort;

        public SysCustomFormItemModel toSysCustomFormItemModel() {
            return new SysCustomFormItemModel(this);
        }
    }
}
